package service

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"lot_admin_api/internal/consts"
	"lot_admin_api/internal/dao"
	"lot_admin_api/internal/model"
	"lot_admin_api/utility/cache"
	"lot_admin_api/utility/response"
)

type sOperate struct{}

var insOperate = sOperate{}

func Operate() *sOperate {
	return &insOperate
}

// Login 登录接口
func (s *sOperate) Login(ctx context.Context, req *model.LoginStruct) (returnInfo *model.OperateIdAndInfoRes, err error) {
	// 手机号验证码登录
	if req.Type == 2 {
		info := &model.IsSMSSuccess{
			Phone:     req.Phone,
			PhoneCode: req.PhoneCode,
		}
		// 校验手机的验证码
		if req.Phone == "17688311401" {
			// 校验手机的验证码
			PhoneCode().IsSMSSuccess(ctx, info)
		}
	} else if req.Type == 3 { // 微信验证登录
		// 这里应该写一个校验 防止暴力登录
	} else if req.Type == 1 {
		// 密码登录
	}
	returnInfo, err = s.PhoneLogin(ctx, req)
	return returnInfo, err
}

// OperateInfo  获取运营用户模块
func (s *sOperate) OperateInfo(ctx context.Context, id string) (res *model.OperateIdAndInfoRes, err error) {
	r := g.RequestFromCtx(ctx)
	if id != "" {
		if err = dao.LotOperate.Ctx(ctx).Where(id).Scan(&res); err != nil {
			response.JsonExit(r, consts.HTTPInternalServer.Code(), "没有当前运营者")
		}
	} else {
		operateInfo := cache.OperateInfo(ctx)
		_ = gconv.Scan(operateInfo, &res)
	}
	return res, err
}

// PhoneLogin 通过手机号登录接口
func (s *sOperate) PhoneLogin(ctx context.Context, req *model.LoginStruct) (res *model.OperateIdAndInfoRes, err error) {
	searchStaffInfo := g.Map{
		"operate_phone": req.Phone,
	}
	r := g.RequestFromCtx(ctx)
	// 获取员工的值
	if err = dao.LotOperate.Ctx(ctx).Where(searchStaffInfo).Scan(&res); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "没有当前运营者")
	}
	return res, err
}
